<?php
require_once('GraphIndex.class.php');
/**
 * ResourceArray
 * a class for providing easier access to a resource, 
 * of the RDF/JSON Resources Array structure.
 * A ResourceArray is the same as a GraphIndex, but has only one Resource in it.
 *
 * @package converters
 * @author Keith Alexander
 **/
class ResourceArray extends GraphIndex
{
	/**
	 * uri: the URI that identifies the resource.
	 *
	 * @var string
	 **/
	public $uri;
	/**
	 * an associative array of property => values belonging to this resource
	 *
	 * @var array
	 **/
	public $properties;
	
	/**
	 * a setter for the value of this data structure
	 * @param array $val - should be an associative array of the rdf/json structure
	 * @return void
	 * @author Keith Alexander
	 **/
	public function set_value($val)
	{
		$this->value = $val;
		$this->uri = array_shift(array_keys($val));
		$this->properties = $val[$this->uri];
	}
	
	/**
	 * value
	 *
	 * @return void
	 * @author Keith Alexander
	 **/
	public function value()
	{
		return array($this->uri => $this->properties);
	}
	
	/**
	 * get_title
	 * @param string $language
	 * @param array $resource
	 * @return string
	 * @author Keith Alexander
	 **/
	public function get_title($language=false, $resource=false)
	{
		if(!$resource) $resource=array($this->uri => $this->properties);
		$resources = new ResourceArray($resource);
		
		if ($language) $filtered_resources = $resources->filter_by_language($language)->to_resource($this->uri);
		else $filtered_resources = $resources;
		
		$title_candidates = array(
			self::DC.'title',
			self::TERMS.'title',
			self::FOAF.'name',
			self::RDFS.'label',
			self::RSS.'title',
			'http://schemas.talis.com/2005/service/schema#name',
			self::FOAF.'mbox',
			);
	
		//try in this language
		foreach($title_candidates as $title_prop)
		{
			if($title = $filtered_resources->property($title_prop) )
			{
				$this->property($title_prop);
				return $title;
			}
		}

		//try in any language
		foreach($title_candidates as $title_prop)
		{
			if($title = $resources->property($title_prop) )
			{
				$lang = isset($this->properties[$title_prop][0]['lang'])? $Resources->to_resource($uri)->get_title(LANGUAGE) : false;
				$this->property($title_prop);
				return $title;
			}
		}

		$uris = array_keys($filtered_resources->value());
		return $uris[0];
	}
	
	/**
	 * property - returns a property of the resource, and unsets it from the data structure
	 * @param string $property - the URI of the property
	 * @param bool $all - indicates whether to return an array of values, or only one 
	 * @return string | array | bool
	 * @author Keith Alexander
	 **/
	public function property($property, $all=false)
	{
		if(isset($this->properties[$property]))
		{
			$objs = $this->properties[$property];
			unset($this->properties[$property]);
			$values = array();
			foreach($objs as $o) $values[]=$o['value'];
			if($all) return $values;
			else return $values[0];
		}
		else
		return false;
	}
	
	/**
	 * object
	 *
	 * @return array
	 * @author Keith Alexander
	 **/
	function object($property=false, $type=false, $lang=false, $datatype=false)
	{
		$return_values = array();
		$properties = $this->properties;
		foreach($properties as $p => $objs)
		{
			if($property == $p OR !$property)
			{
				foreach($objs as $o)
				{
					if(
						($type==$o['type'] OR !$type)
						AND
						(!empty($o['lang']) AND $lang==$o['lang'] OR !$lang)
						AND
						(!empty($o['datatype']) AND $datatype == $o['datatype'] OR !$datatype)
						)
						{
							$return_values[] = $o['value'];
						}
				}
			}
		}
	 return $return_values;
	}
	
	/**
	 * properties - a getter for the resource's properties
	 *
	 * @return void
	 * @author Keith Alexander
	 **/
	function properties()
	{
		return $this->properties;
	}
	
	/**
	 * has_property
	 *
	 * @return boolean
	 * @author Keith Alexander
	 **/
	function has_property($property)
	{
		return isset($this->properties[$property]);
	}
}
?>